<?php
/**
 * User: 五哥派
 * Date: 2024/7/1
 * Tel(同WX): 17335592719
 */
namespace app\api\controller;

use think\Cache;

class AuthBase extends ApiBase
{

    public $token = "";
    public $user_id = 0;
    public $isLogin = false;

    public function _initialize(){
        parent::_initialize();

        $this->token = $this->request->header("token");
        if(!$this->token || !$this->isLogin()) {
            $result = [
                'code' => -1,//状态 -1  token过期
                'msg' => '请联系管理员!',
            ];
            echo json_encode($result, JSON_UNESCAPED_UNICODE);
            die;
        }

    }

    /**
     * 判断用户是否登录 config("redis.token_pre").$token
     * @return bool
     */
    public function isLogin() {
        $user_id = Cache::store('redis')->get(config('redis.token_pre').$this->token);
        //存在该token表示用户登录状态
        if($user_id) {
            $this->isLogin = true;
            $this->user_id = $user_id;
            return true;
        }
        return false;
    }

}